//script per gestire il gioco del tris
//creato da mattia franchetto - 3bi peano


counter=0;
play= false;
fsn=false;

function start(player1,player2){
	if ((player1=="") || (player2=="")){
		document.getElementById("alert").innerHTML="Inserite i vostri nomi prima di iniziare";}
	else{
		document.tris.avvia.disabled=true;
		document.tris.g1.disabled=true;
		document.tris.g2.disabled=true;
		play=true;
		document.getElementById("alert").innerHTML=""};
}

function restart(){
	
/*	document.tris.avvia.disabled=false;
	play=false;
	fsn=false;
	document.tris.g1.disabled=false;
	document.tris.g2.disabled=false;
	document.getElementById("table").style.visibility="visible";
	document.getElementById("c1").innerHTML="";
	document.getElementById("c2").innerHTML="";
	document.getElementById("c3").innerHTML="";
	document.getElementById("c4").innerHTML="";
	document.getElementById("c5").innerHTML="";
	document.getElementById("c6").innerHTML="";
	document.getElementById("c7").innerHTML="";
	document.getElementById("c8").innerHTML="";
	document.getElementById("c9").innerHTML="";
	document.getElementById("alert").innerHTML="";*/
	location.href="index.html";
}
function game(cell,player1,player2){
if (fsn==true){
	document.getElementById("alert").innerHTML="Devi riavviare per giocare!";}
else {
	if (play==true){
		if ((document.getElementById(cell).innerHTML=="X") || (document.getElementById(cell).innerHTML=="O")){
		document.getElementById("alert").innerHTML="Non puoi cliccare su celle già piene.";}
	else{
		document.getElementById("alert").innerHTML="";
	counter=counter+1;
	if (counter%2==0){
		document.getElementById(cell).innerHTML="O";}
	else{
		document.getElementById(cell).innerHTML="X";};
	
	if (((document.getElementById("c1").innerHTML=="X") && (document.getElementById("c2").innerHTML=="X") && (document.getElementById("c3").innerHTML=="X")) || ((document.getElementById("c4").innerHTML=="X") && (document.getElementById("c5").innerHTML=="X") && (document.getElementById("c6").innerHTML=="X")) || ((document.getElementById("c7").innerHTML=="X") && (document.getElementById("c8").innerHTML=="X") && (document.getElementById("c9").innerHTML=="X")) || ((document.getElementById("c1").innerHTML=="X") && (document.getElementById("c5").innerHTML=="X") && (document.getElementById("c9").innerHTML=="X")) || ((document.getElementById("c3").innerHTML=="X") && (document.getElementById("c5").innerHTML=="X") && (document.getElementById("c7").innerHTML=="X")) || ((document.getElementById("c1").innerHTML=="X") && (document.getElementById("c4").innerHTML=="X") && (document.getElementById("c7").innerHTML=="X")) || ((document.getElementById("c3").innerHTML=="X") && (document.getElementById("c6").innerHTML=="X") && (document.getElementById("c9").innerHTML=="X"))){
		document.getElementById("alert").innerHTML=player1+" ha vinto!";
		if (confirm(player1+' ha vinto, volete rigiocare?')){
							restart();}
					else{
							play=false;
							fsn=true;}}
	else{
		if (((document.getElementById("c1").innerHTML=="O") && (document.getElementById("c2").innerHTML=="O") && (document.getElementById("c3").innerHTML=="O")) || ((document.getElementById("c4").innerHTML=="O") && (document.getElementById("c5").innerHTML=="O") && (document.getElementById("c6").innerHTML=="O")) || ((document.getElementById("c7").innerHTML=="O") && (document.getElementById("c8").innerHTML=="O") && (document.getElementById("c9").innerHTML=="O")) || ((document.getElementById("c1").innerHTML=="O") && (document.getElementById("c5").innerHTML=="O") && (document.getElementById("c9").innerHTML=="O")) || ((document.getElementById("c3").innerHTML=="O") && (document.getElementById("c5").innerHTML=="O") && (document.getElementById("c7").innerHTML=="O"))  || ((document.getElementById("c1").innerHTML=="O") && (document.getElementById("c4").innerHTML=="O") && (document.getElementById("c7").innerHTML=="O")) || ((document.getElementById("c3").innerHTML=="O") && (document.getElementById("c6").innerHTML=="O") && (document.getElementById("c9").innerHTML=="O"))){
			document.getElementById("alert").innerHTML=player2+" ha vinto!";
			if (confirm(player2+' ha vinto, volete rigiocare?')){
							restart();}
					else{
							play=false;
							fsn=true;}}
		else{
			if (counter==9){
				document.getElementById("alert").innerHTML="Parità!";
				if (confirm('Parità, volete rigiocare?')){
							restart();}
					else{
							play=false;
							fsn=true;}}
			
					};
				};
			};
		}
	else{
		document.getElementById("alert").innerHTML="Inserite i vostri nomi e premete Start prima di iniziare"};
};

}
